package com.auto.learning.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

@SuppressLint("NewApi")
public abstract class BaseWebViewActivity extends Activity {

	WebView mWebView;
	private Handler baseHandler;
	Application application;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		application = (Application) getApplication();
		init();
		load();
	}

	/**
	 * 初始化（包括view以及数据）
	 * 
	 * @author yansu
	 */
	public abstract void init();

	/**
	 * 加载资源
	 * 
	 * @author yansu
	 */
	public abstract void load();

	/**
	 * 初始化webview，并对WebSettings进行默认设置
	 * 
	 * @param webViewId
	 * @param webViewClient
	 * @param webChromeClient
	 * @author yansu
	 */
	@SuppressLint("SetJavaScriptEnabled")
	protected void initWebView(WebView mWebView,
			BaseWebViewClient webViewClient, BaseWebChromeClient webChromeClient) {
		this.mWebView = mWebView;
		baseHandler = new Handler();

		// mWebView = (WebView) findViewById(webViewId);
		// mWebView.setTag(webViewId, this);

		mWebView.requestFocus();
		mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		// mWebView.setInitialScale(50);//为50%，最小缩放等级

		WebSettings mWebSettings = mWebView.getSettings();
		mWebSettings.setBlockNetworkImage(false); // 是否阻止网络图像
		mWebSettings.setBlockNetworkLoads(false); // 是否阻止网络请求
		mWebSettings.setJavaScriptEnabled(true); // 是否加载JS
		mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
//		mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); // 覆盖方式启动缓存
		mWebSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); // 本地有用本地，否则网络
		mWebSettings.setAppCacheEnabled(true);
		mWebSettings.setUseWideViewPort(false); // 使用广泛视窗
		mWebSettings.setLoadWithOverviewMode(false);
		mWebSettings.setRenderPriority(RenderPriority.NORMAL); // 渲染优先级
		mWebSettings.setDomStorageEnabled(true);
		mWebSettings.setBuiltInZoomControls(false);
		mWebSettings.setSupportZoom(false);
		if (Build.VERSION.SDK_INT >=16) {
			mWebSettings.setAllowFileAccessFromFileURLs(true);
		}
		mWebSettings.setDatabaseEnabled(true);
		String dir = this.getApplicationContext()
				.getDir("database", Context.MODE_PRIVATE).getPath();
		// 设置数据库路径
		mWebSettings.setDatabasePath(dir);

		mWebSettings.setUserAgentString(mWebSettings.getUserAgentString()
				+ ";native-android");// 获得浏览器的环境

		mWebView.setWebViewClient(webViewClient);
		mWebView.setWebChromeClient(webChromeClient);

	}

	/**
	 * 在ui线程中调用mWebView的loadUrl方法
	 * 
	 * @param data
	 * @author yansu
	 */
	public final void loadWebViewData(final String data) {
		baseHandler.post(new Runnable() {
			@Override
			public void run() {
				if (mWebView != null) {
					mWebView.loadUrl(data);
				}
			}
		});
	}

	/**
	 * 获取activity的根布局
	 * 
	 * @param context
	 * @return
	 */
	public View getRootView(Activity context) {
		return ((ViewGroup) context.findViewById(android.R.id.content))
				.getChildAt(0);
	}

	public void changeFont(ViewGroup view, Typeface typeface) {
		for (int i = 0; i < view.getChildCount(); i++) {
			View view2 = view.getChildAt(i);
			if (view2 instanceof TextView) {
				((TextView) view2).setTypeface(typeface);
			} else if (view2 instanceof Button) {
				((Button) view2).setTypeface(typeface);
			} else if (view2 instanceof EditText) {
				((EditText) view2).setTypeface(typeface);
			} else if (view2 instanceof LinearLayout
					|| view2 instanceof RelativeLayout
					|| view2 instanceof FrameLayout) {
				changeFont((ViewGroup) view2, typeface);
			}
		}
	}
}
